package com.sugree.twitter.views;

import java.io.IOException;
import java.util.Vector;
import java.util.Enumeration;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import com.substanceofcode.tasks.AbstractTask;
import com.substanceofcode.utils.Log;
import com.sugree.twitter.TwitterController;
import com.sugree.utils.Loggable;

public class WaitScreen extends Form implements CommandListener, Runnable, Loggable {
	private TwitterController controller;
	private AbstractTask task;
	private int cancelScreen;

	private StringItem waitField;
	private Command cancelCommand;

	private Thread thread;

	public WaitScreen(TwitterController controller, AbstractTask task, int cancelScreen) {
		super("Wait");
		this.controller = controller;
		this.task = task;
		this.cancelScreen = cancelScreen;

		thread = new Thread(this);

		waitField = new StringItem("", "");
		append(waitField);

		cancelCommand = new Command("Cancel", Command.STOP, 1);
		addCommand(cancelCommand);

		setCommandListener(this);
	}

	public void clear() {
		waitField.setText("");
	}

	public void print(String text) {
		waitField.setText(waitField.getText()+text);
	}

	public void println(String text) {
		waitField.setText(waitField.getText()+text+"\n");
	}

	public void setText(String text) {
		waitField.setText(text);
	}

	public void commandAction(Command cmd, Displayable display) {
		if (cmd == cancelCommand) {
			controller.showTimeline();
		}
	}

	public void start() {
		thread.start();
	}

	public void run() {
		Log.setConsole(this);
		task.execute();
		Log.setConsole(null);
	}
}
